﻿#ifndef DEVICECONVERSION_H
#define DEVICECONVERSION_H

#include <QObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonValue>
#include <QSerialPort>

#include <QSerialPortInfo>
#include <QMap>
#include <QPair>
#include <QTimer>

#include "../commons/comalgor.h"

/*
 * 发送给设备指令
*/
class DeviceConversion : public QObject
{
    Q_OBJECT
public:
    explicit DeviceConversion(QObject *parent = nullptr);
    virtual ~DeviceConversion();

    static DeviceConversion * gestance();


    void initComSeriel();
    void closeComSeriel();
    void openComSeriel();


    void handlLed(QMap<int, QPair<int ,int> > leds);

    void handlSequentialDischarge(uint8_t cmds, QList<int> lits, QMap<int, QPair< int ,int >  > infors, QList<int> sorts);
    void handlReginLed(QString regins,int status);
    void handlCmdLed(QMap<int, int >  leds);

    void handlOutCmd();
    void handlSameDischarge(uint8_t cmds, QList<int> lits, QMap<int, QPair< int ,int >  > infors , QList<int> sorts);

    void setCleanIndex(int indexs);

    int getCleanError();

    void handlCleanCmd();
    void handlCloseCmd();
    int getSelectCleanIndexs();
    int getRegnLed(QString regions);
    int getRegnInforsLed(int regions);

    void pauseProgram();
    void goOnProgram();

    QJsonObject currentCleanIndex();
    int currentCleanTime();

    void setCleanError(int errors);

signals:
    void sigSequentialSuccess();
    void sigSamelSuccess();
    void sigCleanSuccess(int errors);
    void sigCloseCmds();
    void sigHandlRunOutTime(int times);
    //运行时间
    void sigCleanRunTimeout(int times);

public slots:
    void onDataReceived();
    void onOutTimeout();
    void onOutCmdTimeout();
    void onCleanTimeout();


private:

    static DeviceConversion * m_deviceConversion;
    QSerialPort * m_serial;

    ComAlgor * m_comAlgor;
    QByteArray m_currentData ="";

    QMap<QString , QList<QPair<int ,int> >  > m_outData;
    QMap<int, QPair< int ,int > > m_outInfors;
    QList<int> m_outList;
    QList<int> m_outSorts;
    QMap<QString, int > m_regnLed;
    QMap<int, int > m_regnInforsLed;
    int m_outTypes;
    QString m_regions;

    QTimer * m_cleanTimer;
    QTimer * m_outTimer;
    QTimer * m_outCmdTimer;
    int m_currentCmd;

    int m_cleanIndexs;
    int m_selectCleanIndexs;
    QList<QJsonObject> m_cleanList;
    int m_clearDataError;

    quint64 m_currentTimes;

};

#endif // DEVICECONVERSION_H
